package com.qhy.springcloud.service.impl;

import com.qhy.springcloud.dao.StorageMapper;
import com.qhy.springcloud.domain.CommonResult;
import com.qhy.springcloud.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author qiuhongyu
 * @date 2020/12/8 10:03
 */
@Service
@Slf4j
public class StorageServiceImpl implements StorageService {

    @Autowired
    private StorageMapper storageMapper;
    /**
     * 减库存
     *
     * @param productId 产品id
     * @param byNum 购买数量
     * @return
     */
    public CommonResult decrease(Long productId, Integer byNum) {
        int i =0;
        try {
            i = storageMapper.decrease(productId, byNum);
        } catch (Exception e) {
           log.error("decrease error", e);
           return new CommonResult(409,"减库存失败！");
        }
        if (i != 1) {
            return new CommonResult(409,"减库存失败！");
        }

        return new CommonResult(200,"减库存成功！");
    }
}
